import { getData } from '@vimi/utils-api'

const state = {
  account: [],
}

const getters = {
  account: state => state.account,
}

const actions = {
  getAccountData({ commit }, force) {
    return new Promise((resolve, reject) => {
      if (!force && state.account.length) {
        return resolve(state.account)
      }
      getData(
        'credit',
        'credit_agency_info',
        {
          'agency_type.eq': 6,
          'org_region_lever.lte': 2,
        },
        { create_time: 'desc' }
      )
        .then(res => {
          const data = (res && res.data) || []
          commit('setAccount', data)
          resolve(data)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
}

const mutations = {
  setAccount: (state, payload) => (state.account = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
